package service

import (
	"encoding/json"
	flow "ez/apps/flow/document"
	"fmt"
	"gitee.com/dreamwood/ez-go/ez"
	"time"
)

func createFlowingPublishTopic(targetType string) string {
	return fmt.Sprintf("ez.Flowing.%s", targetType)
}

func PublishOnChange(flowing *flow.Flowing) {

	message := new(FLowingMessage)
	message.IsConfirm = flowing.IsConfirm
	message.IsCancel = flowing.IsCancel
	message.ApprovalSate = flowing.State
	message.ApprovalStage = flowing.FlowCur.Name
	message.ConfirmAt = flowing.ConfirmAt
	message.TargetId = flowing.TargetId

	e := ez.NsqProducer.Publish(createFlowingPublishTopic(flowing.TargetType), message.Serialize())
	ez.PrintError(e)
}

type FLowingMessage struct {
	TargetId      int64
	IsConfirm     bool      `json:"isConfirm" bson:"isConfirm"`         //是否生效
	ConfirmAt     time.Time `json:"confirmAt" bson:"confirmAt"`         //生效时间
	IsCancel      bool      `json:"isCancel" bson:"isCancel"`           //是否取消
	IsLock        bool      `json:"isLock" bson:"isLock"`               //是否锁定
	ApprovalSate  string    `json:"approvalSate" bson:"approvalSate"`   //评审状态
	ApprovalStage string    `json:"approvalStage" bson:"approvalStage"` //评审环节
}

func (this *FLowingMessage) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *FLowingMessage) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
